﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TmU220PrintHelperV2d0.Entity;
using ZhangLogSysV1d0;

namespace TmU220PrintHelperV2d0.Singleton
{
    public class PrintTempletSetSingletonV2d0
    {
        private static PrintTempletSetSingletonV2d0 _instance = null;
        private static readonly object syslock = new object();
        private static PrintTempletSetV2d0 PrintTempleteSet = null;
        /// <summary>
        /// 单例模式
        /// </summary>
        public static PrintTempletSetSingletonV2d0 Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (syslock)
                    {
                        if (_instance == null)
                        {
                            _instance = new PrintTempletSetSingletonV2d0();
                        }
                    }
                }
                return _instance;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        private PrintTempletSetSingletonV2d0()
        {
            PrintTempleteSet = new PrintTempletSetV2d0();
            LoadData();
        }
        /// <summary>
        /// 装载数据
        /// </summary>
        private void LoadData()
        {
            string currDir = System.IO.Directory.GetCurrentDirectory();
            string filename = currDir + @"\parameter\PrintTempletSet.xml";
            if (File.Exists(filename))
            {
                PrintTempleteSet = PrintTempletSetV2d0.ReadFromFile(filename);
            }
        }
        public PrintTempletSetV2d0 GetPrintTempletSet()
        {
            return PrintTempleteSet;
        }
        public PrintTempletV2d0 GetPrintTemplet(string templetName)
        {
            PrintTempletV2d0 findedTemplet = null;
            try
            {
                findedTemplet = PrintTempleteSet.PrintTempletList.FirstOrDefault(x => x.TempletName == templetName);
                //if (findedTemplet != null) foreach (var item in findedTemplet.PrintTempletRowList) item.PrintMode = item.TempletElementList[0].PrintMode;
            }
            catch (Exception ex)
            {
                SimpleLoger.Instance.Error(ex);
            }
            return findedTemplet;
        }


    }
}
